package com.open.center.jpa.converter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import com.open.center.common.constant.Enumerator;


/**
 * Controller层的枚举转换工厂
 * 支持：@PathVariable, @RequestParam 方式传递枚举参数
 */
public class WebEnumConverterFactory implements ConverterFactory<String, Enumerator<?, ?>> {


    private final Map<Class<?>, Converter<String, ?>> converters = new ConcurrentHashMap<>();

    @Override
    public <T extends Enumerator<?, ?>> Converter<String, T> getConverter(Class<T> targetType) {
        Converter converter = converters.get(targetType);
        if (converter == null) {
            converter = new WebEnumConverter<>(targetType);
            converters.put(targetType, converter);
        }
        return converter;
    }

}
